package fr.pud.client.controller.bubble;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.PUDStation;
import fr.pud.client.model.Route;
import fr.pud.client.model.Routes;
import fr.pud.client.model.Vehicle;

/**
 * Display the information corresponding to the vehicle.
 * Apr 8, 2012 - OSE
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         ShowVehicleInformation.java
 */
public class ShowVehicleInformation extends AbstractAction {
    /**
     * The vehicle linked
     */
    private ArrayList<Vehicle> vehicles;
    /**
     * The serial UID
     */
    private static final long  serialVersionUID = 7504832541311567162L;

    /**
     * Sets up the GUI.
     * @param vehicle
     *            the vehicle to link.
     */
    public ShowVehicleInformation(PUDStation station) {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.VEHICLE));
        this.putValue(SMALL_ICON, ImagesDatabase.TRUCK);
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.VEHICLE_INFORMATION));
        this.vehicles = new ArrayList<Vehicle>();
        for (Route r : Routes.getInstance().getRoutes()) {
            if (r.getStations().contains(station)) {
                this.vehicles.add(r.getVehicle());
            }
        }
    }

    /**
     * Display the information if the controller is activated.
     */
    @Override
    public void actionPerformed(ActionEvent arg0) {
        JOptionPane.showMessageDialog(null, this.prepareString(),
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.VEHICLE_DESCRIPTION),
                JOptionPane.INFORMATION_MESSAGE, ImagesDatabase.TRUCK);
    }

    /**
     * @return a string representation of the vehicle.
     */
    private String prepareString() {
        String toDisplay = "<html>";
        if (this.vehicles != null) {
            int index = 1;
            for (Vehicle v : this.vehicles) {
                toDisplay += "<h3 style=\"color:red\">"
                        + LanguagesConstants.LANGUAGE
                                .getString(LanguagesConstants.VEHICLE) + " "
                        + index++ + " : </h3>";
                if (v != null) {
                    toDisplay += v.toHTML();
                }
                else {
                    toDisplay += LanguagesConstants.LANGUAGE
                            .getString(LanguagesConstants.ERROR_OCCURED)
                            + "<br/>";
                }
            }
        }
        else {
            toDisplay = LanguagesConstants.LANGUAGE
                    .getString(LanguagesConstants.ERROR_OCCURED);
        }
        toDisplay += "</html>";
        return toDisplay;
    }
}
